<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

$root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
if (file_exists($root.'/wp-load.php')) {
    // WP 2.6
    require_once($root.'/wp-load.php');
} else {
    // Before 2.6
    require_once($root.'/wp-config.php');
}

$id_num = (int)$_GET['widget_id'];

define_whatsits_config();

$database = new Wordpress_Database('default_username');

$whatsit = $database->getWhatsit($id_num);

$pref_array = $whatsit->getPreferenceArray();

?>
<html>
<head>
<title>Edit Preferences for <?= $whatsit->getTitle() ?></title>
</head>
<body>
<form method="POST" action="saveprefs.php">
<input type="hidden" name="whatsit_id" value="<?= $id_num ?>"/>
<table>
<?php foreach ($pref_array as $name=>$p) : ?>
	<tr>

	<?php if ($p['type'] == 'string') : ?>
		<td align="right"><?= $p['display'] ?></td>
		<td><input type="text" name="prefs[<?= $name ?>]" value="<?= $p['value'] ?>"/></td>

	<?php elseif ($p['type'] == 'bool') : ?>
		<td align="right"><?= $p['display'] ?></td>
		<td><input type="checkbox" name="prefs[<?= $name ?>]" <?php if ($p['value'] == true) : ?> checked="CHECKED"<?php endif; ?>/></td>

	<?php elseif ($p['type'] == 'enum') : ?>
		<td align="right"><?= $p['display'] ?></td>
		<td><select name="prefs[<?= $name ?>]">
				<?php foreach ($p['options'] as $o) : ?>
					<option value="<?= $o['value'] ?>" <?php if ($p['value'] == $o['value']) echo "SELECTED"; ?>><?= $o['display'] ?></option>
				<?php endforeach; ?>	
			</select>
		</td>

	<?php elseif ($p['type'] == 'hidden') : ?>
		<td align="right"></td>
		<td><input type="hidden" name="prefs[<?= $name ?>]" value="<?= $p['value'] ?>"/></td>	
	<?php endif; ?>

	</tr>
<?php endforeach; ?>
</table>
<input type="Submit" value="Submit"/><input type="button" value="Cancel" onclick="self.parent.tb_remove();"/>
</form>
</body>
</html>